-- 模拟器 dy-pay-shiming
require("TSLib")
local ts 				= require('ts')
local sz 				= require("sz")
local json 				= ts.json
local ts_enterprise_lib = require("ts_enterprise_lib")

local model 			= {}

model.idCard_user       = ""
model.idCard_numCode    = ""

function model:click(click_x, click_y, ms)
	mSleep(math.random(200, 300))
	randomTap(click_x, click_y, 5)
	mSleep(ms and ms or math.random(500, 600))
end

function model:myToast(str,ms)
	toast(str,1)
	mSleep(ms and ms or 1000)
end

function model:getIdCardInfo()
	::get_idCard_data::
	local category = "idCard-data"
	ts_enterprise_lib.timeout = 25
	local plugin_ok,api_ok,data = ts_enterprise_lib:plugin_api_call("DataCenter","get_data",category)
	if plugin_ok and api_ok then
		self:myToast(data)
		local data = strSplit(data,"----")
		if #data > 0 then
			self.idCard_user = data[1]
			self.idCard_numCode = data[2]
		else
			self:myToast("身份信息已经用完，请重新上传新数据")
			goto get_idCard_data
		end	
	else
		self:myToast("sfz号码获取失败或者数据已经用完，请重新上传新数据:"..tostring(plugin_ok).."----"..tostring(api_ok))
		goto get_idCard_data
	end
end

function model:checkIdCard()
	while (true) do
		mSleep(50)
		x,y = findMultiColorInRegionFuzzy( 0x04498d, "2|5|0x04498d,43|5|0x04498d,101|6|0x04498d,112|6|0x04498d,131|6|0x04498d,-100|70|0xdcddde,332|71|0xdcddde,92|23|0xffffff,-9|-384|0x161823", 90, 0, 0, 539, 959)
		if x1 ~= -1 then
			self:click(x + 305, y - 204)
			inputText(self.idCard_user)
			mSleep(500)
			self:click(x + 305, y - 87)
			inputText(self.idCard_numCode)
			mSleep(500)
		end

		mSleep(50)
		if getColor(136,  621) == 0xfe2c55 then
			self:click(136,  621)
			break
		end
	end

	while (true) do   
		mSleep(50)
		if getColor(136,  621) == 0xfe2c55 then
			self:click(136,  621)
		end
		
		--抖音支付密码
		mSleep(50)
		x,y = findMultiColorInRegionFuzzy( 0x161823, "-13|5|0x161823,-11|13|0x161823,10|13|0x161823,26|19|0x161823,45|19|0x161823,70|21|0x161823,70|2|0x161823,99|19|0x161823,110|14|0x161823", 90, 0, 0, 539, 959)
		if x ~= -1 then
			mSleep(1000)
			os.execute("input keyevent KEYCODE_BACK")
			category = "success-data"
			data = self.idCard_user .. "----" .. self.idCard_numCode .. "----成功"
			break
		end

		--知道了
		mSleep(50)
		x,y = findMultiColorInRegionFuzzy( 0x1a74ff, "-2|7|0x1a74ff,9|7|0x1a74ff,16|7|0x1a74ff,25|10|0x1a74ff,34|8|0x1a74ff,56|12|0x1a74ff,56|-1|0x1a74ff,37|18|0x1a74ff", 90, 0, 0, 539, 959)
		if x ~= -1 then
			self:click(x, y, 1000)
			category = "error-data"
			data = self.idCard_user .. "----" .. self.idCard_numCode .. "----失败"
			break
		end
	end

	while (true) do
		mSleep(50)
		x,y = findMultiColorInRegionFuzzy( 0x04498d, "2|5|0x04498d,43|5|0x04498d,101|6|0x04498d,112|6|0x04498d,131|6|0x04498d,-100|70|0xdcddde,332|71|0xdcddde,92|23|0xffffff,-9|-384|0x161823", 90, 0, 0, 539, 959)
		if x ~= -1 then
			mSleep(500)
			break
		else
			self:click(461,  346)
			self:click(461,  346)
			self:click(460,  461)
			self:click(460,  461)
		end
		
		--抖音支付密码
		mSleep(50)
		x,y = findMultiColorInRegionFuzzy( 0x161823, "-13|5|0x161823,-11|13|0x161823,10|13|0x161823,26|19|0x161823,45|19|0x161823,70|21|0x161823,70|2|0x161823,99|19|0x161823,110|14|0x161823", 90, 0, 0, 539, 959)
		if x ~= -1 then
			mSleep(1000)
			os.execute("input keyevent KEYCODE_BACK")
		end

		--知道了
		mSleep(50)
		x,y = findMultiColorInRegionFuzzy( 0x1a74ff, "-2|7|0x1a74ff,9|7|0x1a74ff,16|7|0x1a74ff,25|10|0x1a74ff,34|8|0x1a74ff,56|12|0x1a74ff,56|-1|0x1a74ff,37|18|0x1a74ff", 90, 0, 0, 539, 959)
		if x ~= -1 then
			self:click(x, y, 1000)
		end
	end

	::pushData::
	ts_enterprise_lib.timeout = 25
	local plugin_ok,api_ok,data = ts_enterprise_lib:plugin_api_call("DataCenter","insert_data",category,data)
	if plugin_ok or api_ok then
		self:myToast("插入数据成功")
	else
		self:myToast("插入数据失败，重新插入:"..tostring(plugin_ok).."----"..tostring(api_ok))
		goto pushData
	end
end

function model:main()
	while (true) do
		self:getIdCardInfo()
		self:checkIdCard()
	end
end

model:main()